using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using Lucene.Net.Search;
using Lucene.Net.QueryParsers;
using Lucene.Net.Documents;
using Lucene.Net.Analysis.Standard;

namespace GURUCORE.Framework.Core.Indexing
{
	public class Searcher
	{
		private IndexSearcher m_oIndexSearcher;
		public Searcher()
		{
			m_oIndexSearcher = new IndexSearcher(GApplication.GetInstance().RootDir + "/Data/System/Index");
		}

		public Searcher(string p_sFolder)
		{
			m_oIndexSearcher = new IndexSearcher(p_sFolder);
		}

		public int[] Search(string p_sTable, string p_sTerm)
		{
			string sDomain = string.Empty;
			string sTableName = p_sTable.ToLower();
			if ((p_sTable.IndexOf("_") >= 0) && (p_sTable.IndexOf("_") < p_sTable.Length - 1))
			{
				string[] arrToken = p_sTable.Split('_');
				sDomain = arrToken[0].ToLower();
				sTableName = arrToken[1].ToLower();
			}

			StandardAnalyzer oAnalyzer = new StandardAnalyzer();

			QueryParser oParser = new QueryParser("__Content__", oAnalyzer);
			Query oMainQuery = oParser.Parse(p_sTerm);

			TermQuery oDomainTermQuery = new TermQuery(new Lucene.Net.Index.Term("__Domain__", sDomain));
			TermQuery oDocumentTermQuery = new TermQuery(new Lucene.Net.Index.Term("__Document__", sTableName));

			BooleanQuery oTargetQuery = new BooleanQuery();
			oTargetQuery.Add(oDomainTermQuery, BooleanClause.Occur.MUST);
			oTargetQuery.Add(oDocumentTermQuery, BooleanClause.Occur.MUST);

			QueryFilter oFilter = new QueryFilter(oTargetQuery);
			
			Hits oResult = m_oIndexSearcher.Search(oMainQuery ,oFilter,Sort.RELEVANCE);
			List<int> arrID = new List<int>();

			for (int nIdx = 0; nIdx < oResult.Length(); nIdx++)
			{
				Document oDoc = oResult.Doc(nIdx);
				string sDocumentID = oDoc.Get("__ID__");
				try
				{
					arrID.Add(Convert.ToInt32(sDocumentID));
				}
				catch
				{

				}
			}
			return arrID.ToArray();
		}

		public Hashtable SearchWithRelevancy(string p_sTable, string p_sTerm)
		{
			string sDomain = string.Empty;
			string sTableName = p_sTable.ToLower();
			if ((p_sTable.IndexOf("_") >= 0) && (p_sTable.IndexOf("_") < p_sTable.Length - 1))
			{
				string[] arrToken = p_sTable.Split('_');
				sDomain = arrToken[0].ToLower();
				sTableName = arrToken[1].ToLower();
			}

			StandardAnalyzer oAnalyzer = new StandardAnalyzer();

			QueryParser oParser = new QueryParser("__Content__", oAnalyzer);
			Query oMainQuery = oParser.Parse(p_sTerm);

			TermQuery oDomainTermQuery = new TermQuery(new Lucene.Net.Index.Term("__Domain__", sDomain));
			TermQuery oDocumentTermQuery = new TermQuery(new Lucene.Net.Index.Term("__Document__", sTableName));

			BooleanQuery oTargetQuery = new BooleanQuery();
			oTargetQuery.Add(oDomainTermQuery, BooleanClause.Occur.MUST);
			oTargetQuery.Add(oDocumentTermQuery, BooleanClause.Occur.MUST);

			QueryFilter oFilter = new QueryFilter(oTargetQuery);

			Hits oResult = m_oIndexSearcher.Search(oMainQuery, oFilter, Sort.RELEVANCE);
			Hashtable htID = new Hashtable();

			for (int nIdx = 0; nIdx < oResult.Length(); nIdx++)
			{
				Document oDoc = oResult.Doc(nIdx);
				string sDocumentID = oDoc.Get("__ID__");
				float fScore = oResult.Score(nIdx);
				try
				{
					htID.Add(sDocumentID,fScore);
				}
				catch
				{

				}
			}
			return htID;
		}

		public void Commit()
		{
			m_oIndexSearcher.Close();
		}
	}
}
